package top.noteday.controller;

import entity.form.FormDto;
import entity.form.FormVo;
import entity.result.EntityResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.noteday.provider.form.FormService;
import utils.common.StrUtil;

import java.util.Date;

@RestController
@RequestMapping(value = "/backstage/form")
public class FormController {
    private final Logger LOGGER = LoggerFactory.getLogger(FormController.class);

    @Autowired
    private FormService formService;

    /**
     * 保存表单模板
     * {
     * 	"name":"自定义测试第一个表单",
     * 	"code":"firstForm",
     * 	"activationType":1,
     *
     * 	"fieldList":[
     *                {
     * 			"name": "客户姓名2",
     * 			"code": "borrowerName",
     * 			"fieldType": 1,
     * 			"controls": 1,
     *        },
     *        {
     * 			"name": "客户电话2",
     * 			"code": "borrowerPhone",
     * 			"fieldType": 1,
     * 			"controls": 2,
     *        }
     * 	]
     * }
     */
    @PostMapping("/saveFormTemplate")
    public EntityResult<FormDto> saveFormTemplate(@RequestBody FormVo vo) {
        LOGGER.info("保存表单模板{} = ", vo);

        vo.setUserId(0);
        vo.setDate(new Date());

        return formService.saveFormTemplate(vo);
    }

    /**
     * 获取表单模板
     * {
     * 	"id":15
     * }
     */
    @PostMapping("/getFormTemplate")
    public EntityResult<FormDto> getFormTemplate(@RequestBody FormVo vo) {
        LOGGER.info("获取表单模板{} = ", vo);

        StrUtil.paramIsCorrect(StrUtil.isNotEmpty(vo.getId()), "模板主键不能为空");

        return formService.getFormTemplate(vo);
    }

    /**
     * 提交表单
     * {
     * 	"formId":15,
     * 	"code":"firstForm",
     * 	"fieldMap":{
     * 		"borrowerName":"马琳",
     * 		"borrowerPhone":"15500000011"
     *        }
     * }
     */
    @PostMapping("/submitForm")
    public EntityResult<FormDto> submitForm(@RequestBody FormVo vo) {
        LOGGER.info("提交表单{} = ", vo);

        StrUtil.paramIsCorrect(StrUtil.isNotEmpty(vo.getFormId()), "模板主键不能为空");

        return formService.submitForm(vo);
    }
}
